package Lesson;

import javax.swing.*;

public class ClassATMPro {
    // 账号
    static String[] userAccounts = {"a","b","c","d"};
    // 密码
    static String[] userPasswords = {"1111", "2222", "3333","1234"};
    // 余额
    static int[] userMoneys = {123, 1234, 12345,8888};
    //登陆账号下标
    static int index=-1;
    //控制系统运行
    static boolean isRun=true;
    static int transNum=0;
    public static void main(String[] args){
       init();
    }
    /**
     * 初始化
     */
    public static void init(){
        JOptionPane.showMessageDialog(null,"欢迎光临xx银行");
        if (!login()){
            JOptionPane.showMessageDialog(null,"登陆失败，非法用户");
            System.exit(0);
        }
        while (isRun){
            JOptionPane.showMessageDialog(null,"选择你要进行的操作");
            String choose=JOptionPane.showInputDialog(null,"1-存钱\n2-查询\n3-取钱\n4-转账\n5-修改密码\n6-退出系统");
            switch (choose){
                case "1":
                    moneySave();
                    break;
                case "2":
                    informGet();
                    break;
                case "3":
                    moneyGet();
                    break;
                case "4":
                    moneyTrans();
                    break;
                case "5":
                    psdChange();
                    break;
                case "6":
                    JOptionPane.showMessageDialog(null,"谢谢使用");
                    System.exit(0);
            }


        }

    }

    //修改密码
    public static void psdChange(){
        String oldPsd=JOptionPane.showInputDialog(null,"输入你的旧密码");
        if (!oldPsd.equals(userPasswords[index])){
            JOptionPane.showMessageDialog(null,"输入的原密码有误，非法操作");
            return;
        }
        String newPsd=JOptionPane.showInputDialog(null,"输入新密码");
        String rePsd=JOptionPane.showInputDialog(null,"再次输入新密码");
        if(!newPsd.equals(rePsd)){
            JOptionPane.showMessageDialog(null,"输入的新密码不一致，非法操作");
            return;
        }
        userPasswords[index]=newPsd;
        JOptionPane.showMessageDialog(null,"密码修改成功");

    }

    //转账
    public static void moneyTrans(){
        boolean flag;
        boolean isEnough=true;
        do{
            flag=true;
            String str=JOptionPane.showInputDialog(null,"输入对方账号");
            if (str.equals(userAccounts[index])||!countRight(str)){
                JOptionPane.showMessageDialog(null,"对方账号有误，重新输入");
                flag=false;
            }
        }while (!flag);
        String t=JOptionPane.showInputDialog(null,"输入转账金额");
        int trans=Integer.parseInt(t);
        if (trans>userMoneys[index]){
            JOptionPane.showMessageDialog(null,"余额不足，无法转账");
            return;
        }
        JOptionPane.showMessageDialog(null,"转账成功");
        userMoneys[index]-=trans;
        userMoneys[transNum]+=trans;
        informGet();


    }

    public static boolean countRight(String str){
        for (int i=0;i<userAccounts.length;i++){
            if(str.equals(userAccounts[i])){
                int transNum=i;
                return true;
            }
        }
        return false;
    }

    //取钱
    public static void moneyGet(){
        String s=JOptionPane.showInputDialog(null,"输入你的取款金额");
        int get=Integer.parseInt(s);
        if (get>userMoneys[index]){
            JOptionPane.showMessageDialog(null,"余额不足");
            return;
        }
        userMoneys[index]-=get;
        informGet();
    }

    //查询
    public static void informGet(){
        JOptionPane.showMessageDialog(null,"你的账号余额为"+userMoneys[index]);
    }

    //存钱
    public static void moneySave(){
        String input=JOptionPane.showInputDialog(null,"输入存款金额");
        int inputMoney=Integer.parseInt(input);
        userMoneys[index]+=inputMoney;
        JOptionPane.showMessageDialog(null,"存款成功");
        informGet();
    }

    //登录、判断账号
    public static boolean login(){
        for (int i=0;i<3;i++){
            String inputCount=JOptionPane.showInputDialog(null,"输入你的账号");
            String inputPsd=JOptionPane.showInputDialog(null,"输入你的密码");
            if (checkCount(inputCount,inputPsd)){
                return true;
            }
        }
        return false;
    }

     public static boolean checkCount(String inputCount, String inputPsd){
        for (int i=0;i<userAccounts.length;i++){
            if(inputCount.equals(userAccounts[i])&&inputPsd.equals(userPasswords[i])){
                index=i;
                return true;
            }
        }
        JOptionPane.showMessageDialog(null,"账号或密码有误");
        return false;
    }
}

